
from ihome.libs.yuntongxun.CCPRestSDK import REST
# import ConfigParser

#主帐号
accountSid= '8aaf070871bf69700171e7465cc40b77'

#主帐号Token
accountToken= '3ef4ba9e5e7d441fac4dba70cf81e049'

#应用Id
appId='8aaf070871bf69700171e7465d240b7e'

#请求地址，格式如下，不需要写http://
serverIP='app.cloopen.com'

#请求端口 
serverPort='8883'

#REST版本号
softVersion='2013-12-26'

  # 发送模板短信
  # @param to 手机号码
  # @param datas 内容数据 格式为数组 例如：{'12','34'}，如不需替换请填 ''
  # @param $tempId 模板Id

class CCP:
    '''为了保证初始化仅执行一次，所以采用单例模式'''
    instance = None

    def __new__(cls):
        '''单例模式'''
        if cls.instance is None:
            obj = super(CCP, cls).__new__(cls)

            obj.rest = REST(serverIP, serverPort, softVersion)
            obj.rest.setAccount(accountSid, accountToken)
            obj.rest.setAppId(appId)

            cls.instance = obj
        return cls.instance

    # def __init__(self):
    #     self.reset = REST(serverIP,serverPort,softVersion)
    #     self.reset.setAccount(accountSid, accountToken)
    #     self.reset.setAppId(appId)

    def send_sms_template(self,to,datas,tempId):
        result = self.rest.sendTemplateSMS(to, datas, tempId)
        # for k, v in result.items():
        #
        #     if k == 'templateSMS':
        #         for k, s in v.items():
        #             print('%s:%s' % (k, s))
        #     else:
        #         print('%s:%s' % (k, v))
        # statusCode: 000000
        # smsMessageSid: 6084e98
        # e35614b9e9fa7297d35af184e
        # dateCreated: 20200506103547
        statusCode = result.get("statusCode")
        print(result)
        if statusCode == "000000":
            # 发送成功
            return 0
        else:
            # 发送失败
            return -1


if __name__ == '__main__':
    ccp = CCP()
    ret=ccp.send_sms_template("17611640600", ["web开发测试","123"],1)
    print(ret)


'''
def sendTemplateSMS(to,datas,tempId):
    
    #初始化REST SDK，每次调用函数都需要初始化，浪费资源
    rest = REST(serverIP,serverPort,softVersion)
    rest.setAccount(accountSid,accountToken)
    rest.setAppId(appId)
    
    result = rest.sendTemplateSMS(to,datas,tempId)
    for k,v in result.iteritems(): 
        
        if k=='templateSMS' :
                for k,s in v.iteritems(): 
                    print '%s:%s' % (k, s)
        else:
            print '%s:%s' % (k, v)
'''
   
#sendTemplateSMS(手机号码,内容数据,模板Id)